export default function levelOrder(root, result=[]) {
  if(!root) return result;
  const queue = [root];
  while(queue.length) {
    const node = queue.shift();
    result.push(node.val);
    if(node.left) queue.push(node.left);
    if(node.right) queue.push(node.right);
  }
  return result;
}

const levelOrder2 = (rootArr, result = []) => {
  if (!root) return result;
  const queue = [root];
  while (queue.length) {
    const level = [];
    const size = queue.length;
    for (let i = 0; i < size; i++) {
      const node = queue.shift();
      level.push(node.val);
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
    }
    result.push(level);
  }
  return result;
};